using System;
using System.Collections.Generic;
using Castle.ActiveRecord.Framework;
using NHibernate.Expression;

namespace CosyTrade.Data
{
    public class OrmDataAccess
    {
        public static void Initialize(IConfigurationSource source, params Type[] ormTypes)
        {
            DataAccessBase.Initialize(source, ormTypes);
        }

        public static void Create(BusinessObject instance)
        {
            DataAccessBase.Create(instance);
        }

        public static void Save(BusinessObject instance)
        {
            DataAccessBase.Save(instance);
        }

        public static void Delete(BusinessObject instance)
        {
            DataAccessBase.Delete(instance);
        }

        public static T FindByPrimaryKey<T>(Guid primaryKey)
        {
            return DataAccessBase.FindByPrimaryKey<T>(primaryKey);
        }

        public static object FindByPrimaryKey(Type type, Guid primaryKey)
        {
            return DataAccessBase.FindByPrimaryKey(type, primaryKey);
        }

        public static T FindOne<T>(params ICriterion[] criterion)
        {
            return DataAccessBase.FindOne<T>(MixCriterion(criterion));
        }

        public static T FindOne<T>(DetachedCriteria criterion) where T : class
        {
            if (criterion != null)
                criterion.Add(Expression.Eq("IsDeleteRecord", false));
            return DataAccessBase.FindOne<T>(criterion);
        }

        public static IList<T> FindAll<T>(params ICriterion[] criterias)
        {
            return DataAccessBase.FindAll<T>(MixCriterion(criterias));
        }

        public static IList<T> FindAll<T>(Order[] orderBy, params ICriterion[] criterias)
        {
            return DataAccessBase.FindAll<T>(orderBy, MixCriterion(criterias));
        }

        public static IList<T> SlicedFindAll<T>(int firstResult, int maxResult, params ICriterion[] criterias)
        {
            return DataAccessBase.SlicedFindAll<T>(firstResult, maxResult, MixCriterion(criterias));
        }

        public static IList<T> SlicedFindAll<T>(int firstResult, int maxResult, Order[] orderBy, params ICriterion[] criterias)
        {
            return DataAccessBase.SlicedFindAll<T>(firstResult, maxResult, orderBy, MixCriterion(criterias));
        }

        public static object UniqueResult(Type type, DetachedCriteria detachedCriteria)
        {
            detachedCriteria.Add(Expression.Eq("IsDeleteRecord", false));
            return DataAccessBase.UniqueResult(type, detachedCriteria);
        }

        private static ICriterion[] MixCriterion(params ICriterion[] criterias)
        {
            List<ICriterion> criLst = new List<ICriterion>();
            criLst.Add(Expression.Eq("IsDeleteRecord", false));
            if(criterias!=null)
                foreach (ICriterion criteria in criterias)
                    if(criteria!=null) criLst.Add(criteria);
            return criLst.ToArray();
        }
    }
}